import { createRouter, createWebHashHistory } from "vue-router";
// 1. 配置路由
const routes: any = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: () => import('../views/login/index.vue') },
  {
    path: '/layout',
    redirect: '/home',
    component: () => import('../layout/index.vue'),
    children: [
      {
        path: '/home',
        component: () => import('../views/home/index.vue'),
        meta: {
          icon: '',
          title: '用户管理'
        }
      },
      {
        path: '/user',
        component: () => import('../views/authority/user/index.vue'),
        meta: {
          icon: '',
          title: '用户管理'
        }
      },
      {
        path: '/role',
        component: () => import('../views/authority/role/index.vue'),
        meta: {
          icon: '',
          title: '角色管理'
        }
      }
    ]
  }

];
// 2.返回一个 router 实列，为函数，配置 history 模式
const router = createRouter({
  history: createWebHashHistory(),
  routes,
});


// 添加前置路由守卫
router.beforeEach((to, _from, next) => {
  const token = localStorage.getItem('token')

  if (to.path == '/home') {
    if (token !== null && token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})


// 3.导出路由   去 main.ts 注册 router.ts
export default router

